# Copyright (c) 2008, Eamonn Faherty
# This is licensed under the MIT licence.

import unittest,sys,os
sys.path.append( os.path.abspath('../') )

from checkstyleas3 import asCheckStyle
from checkstyleas3 import asModel

import asBuilder, asCheckStyle

class ASCheckStyleTestCaseForInterface(unittest.TestCase):
	def testCheckInterfaceIsTypeFail(self):
		"Check that InterfaceIsType gives a negative"
		score = asCheckStyle.CheckScore()
		interface = asModel.ASClass();
		interface.isInterface = True
		check = asCheckStyle.CheckInterface()
		self.assertEqual(-1,check.checkInterfaceIsType(interface, score))

	def testCheckInterfaceIsTypePass(self):
		"Check that InterfaceIsType gives a positive"
		score = asCheckStyle.CheckScore()
		interface = asModel.ASClass()
		interface.isInterface = True
		interface.methods = [asModel.ASMethod("foo","String"), asModel.ASMethod("bar","String")]
		check = asCheckStyle.CheckInterface()
		self.assertEqual(0,check.checkInterfaceIsType(interface, score))

class ASCheckStyleTestCaseForPackage(unittest.TestCase):
	def testCheckAvoidStarImportPass(self):
		"Check that AvoidStarImportPass gives a positive"
		score = asCheckStyle.CheckScore()
		package = asModel.ASPackage()
		package.imports = ["uk.co.doughball.ui.Button"]
		check = asCheckStyle.CheckPackage()
		self.assertEqual(0, check.checkAvoidStarImport(package, score))

	def testCheckAvoidStarImportFail(self):
		"Check that AvoidStarImportPass gives a negative"
		score = asCheckStyle.CheckScore()
		package = asModel.ASPackage()
		package.imports = ["uk.co.doughball.ui.*"]
		check = asCheckStyle.CheckPackage()
		self.assertEqual(-1, check.checkAvoidStarImport(package, score))

class ASCheckStyleTestCaseForMethod(unittest.TestCase):
	def testCheckParameterNumberPass(self):
		"Check that CheckParameterNumberPass gives a positive"
		score = asCheckStyle.CheckScore()
		method = asModel.ASMethod();
		type = asModel.ASType("bar","Bar")
		var = asModel.ASVariable('foo',type)
		method.arguments = {'foo':var}
		check = asCheckStyle.CheckMethod()
		self.assertEqual(0, check.checkParameterNumber(method, score))

	def testCheckParameterNumberFail(self):
		"Check that CheckParameterNumberPass gives a negative"
		score = asCheckStyle.CheckScore()
		method = asModel.ASMethod();
		type = asModel.ASType("bar","Bar")
		var = asModel.ASVariable('foo',type)
		method.arguments = {'foo':var, 'bar':var, 'oo':var, 'hello':var, 'world':var, 'arghh':var , 'fub':var , 'baar':var }
		check = asCheckStyle.CheckMethod()
		self.assertEqual(-1, check.checkParameterNumber(method, score))

class ASCheckStyleTestCaseForCheckScore(unittest.TestCase):
	def testAddPackageScoreForASingleCheck(self):
		"Check that addpackageScore adds to the score"
		checkScore = asCheckStyle.CheckScore()
		check = asCheckStyle.CheckPackage()
		package = asModel.ASPackage()
		package.imports = ["uk.co.doughball.ui.*"]
		score = check.checkAvoidStarImport(package, checkScore)
		self.assertEqual(score, checkScore.score)

	def testAddPackageScoreForAMultipleChecks(self):
		"Check that multiple addpackageScore adds to the score"
		checkScore = asCheckStyle.CheckScore()
		check = asCheckStyle.CheckPackage()
		package = asModel.ASPackage()
		package.imports = ["uk.co.doughball.ui.*"]
		score = check.checkAvoidStarImport(package, checkScore)
		score += check.checkAvoidStarImport(package, checkScore)
		score += check.checkAvoidStarImport(package, checkScore)
		self.assertEqual(score, checkScore.score)

	def testCheckInterfaceIsType(self):
		"Check that checkInterfaceIsType adds to the score"
		score = asCheckStyle.CheckScore()
		interface = asModel.ASClass();
		interface.isInterface = True
		check = asCheckStyle.CheckInterface()
		checkResult = check.checkInterfaceIsType(interface, score)
		self.assertEqual(score.score,checkResult)

	def testCheckInterfaceIsTypeForMultipleChecks(self):
		"Check that checkInterfaceIsType adds to the score for multiples"
		score = asCheckStyle.CheckScore()
		interface = asModel.ASClass();
		interface.isInterface = True
		check = asCheckStyle.CheckInterface()
		checkResult = check.checkInterfaceIsType(interface, score)
		checkResult += check.checkInterfaceIsType(interface, score)
		checkResult += check.checkInterfaceIsType(interface, score)
		self.assertEqual(score.score,checkResult)
		
	def testCheckParameterNumber(self):
		"Check that CheckParameterNumberPass adds to the score"
		score = asCheckStyle.CheckScore()
		method = asModel.ASMethod();
		type = asModel.ASType("bar","Bar")
		var = asModel.ASVariable('foo',type)
		method.arguments = {'foo':var, 'bar':var, 'oo':var, 'hello':var, 'world':var, 'arghh':var , 'fub':var , 'baar':var }
		check = asCheckStyle.CheckMethod()
		checkResult = check.checkParameterNumber(method, score)
		self.assertEqual(score.score, checkResult)

	def testCheckParameterNumberMultiples(self):
		"Check that CheckParameterNumberPass adds to the score for multiples"
		score = asCheckStyle.CheckScore()
		method = asModel.ASMethod();
		type = asModel.ASType("bar","Bar")
		var = asModel.ASVariable('foo',type)
		method.arguments = {'foo':var, 'bar':var, 'oo':var, 'hello':var, 'world':var, 'arghh':var , 'fub':var , 'baar':var }
		check = asCheckStyle.CheckMethod()
		checkResult = check.checkParameterNumber(method, score)
		checkResult += check.checkParameterNumber(method, score)
		checkResult += check.checkParameterNumber(method, score)
		self.assertEqual(score.score, checkResult)

if __name__ == "__main__":
	unittest.main()